<?php
require 'include/library.inc.php';

$sef_url = escape(($_GET['sef'])?$_GET['sef']:$_POST['sef_url']);
if (!$sef_url) redirect("schools.php");

if ($_POST['is_submitted']) {
	
	if (!$_POST['course_date_id']) {
		
		redirect ("school/".$sef_url."#courses");
		
	} else {
		
		$_SESSION['course_order']['school_id'] = $_POST['school_id'];
		$_SESSION['course_order']['course_id'] = $_POST['course_id'];
		$_SESSION['course_order']['course_date_id'] = $_POST['course_date_id'];
		
		redirect("order.php");
	
	}
}

$sql = 'SELECT
			s.*,
			c.name AS city,
			c.state,
			d.name AS country
		FROM
			schools AS s,
			cities AS c,
			countries AS d
		WHERE
		  	sef_url = \''.$sef_url.'\'
		AND
			s.city_id = c.city_id
		AND
			c.country_id = d.country_id
		';
$row_school = mysql_fetch_assoc(query($sql));

if(!$row_school['school_id']) redirect("schools.php");



if ($_POST['is_review']) {
	$error_message = '';
	$message = '';
	
	if (!$_POST['review']) $error_message .= 'Please add a comment!<br/>';
	if (!$_POST['rating1'] || !$_POST['rating2'] || !$_POST['rating3'] || !$_POST['rating4'] || !$_POST['rating5'] || !$_POST['rating6']) $error_message .= 'Missing stars!<br/>';
	

	if (!$error_message) {
		
			
			$sql = "INSERT INTO schools_reviews
					 (school_id,user_id,review,facilities,location,teacher,personal,social,safety,published)
				   VALUES
					 ('".$row_school['school_id']."','".$_SESSION['USER_SID']."','".escape($_POST['review'])."','".$_POST['rating4']."','".$_POST['rating5']."','".$_POST['rating1']."','".$_POST['rating2']."','".$_POST['rating3']."','".$_POST['rating6']."','1')
				   
				";
			query($sql);
			
			$message = 'Thank you!';	
			
			
			
			if($_SESSION['FB_ID'])
			{
			require 'include/fbmain.inc.php';
			$status  = text_format($_POST['review'],200).'...';
            $link = 'http://esltraveler.com/school/'.$sef_url;
			$statusUpdate = $facebook->api('/me/feed', 'post', array('link'=>$link, 'message'=> $status, 'cb' => ''));
			}
            
		
	}
}


head(sth($row_school['name'])." - ESL Traveler.com",
	sth($row_school['meta_description']),
	100,
	"school/".$sef_url,
	$row_school['logo']);
?>


<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="<?=HTML_BASE?>js/map.js"></script>
 

        <div id="left">
        
        	<h2><?=sth($row_school['name'])?></h2>
            
            <div class="padding" style="min-height:640px;">
            	
            	<?=stripslashes($row_school['description'])?>
            </div>
        </div>
        
        <div class="right">
        	<h2>Images</h2>
            
			<script src="<?=HTML_BASE?>js/jquery.carouFredSel-6.2.0-packed.js" type="text/javascript"></script>
			<script type="text/javascript">
            $(document).ready(function() {
                $("#slider").carouFredSel({
                    items       : 1,
					width: 300,
					height: 250,
                    auto : {
                        fx 			: "fade",
                        easing      : "linear",
                        duration    : 1000,
                        timeoutDuration: 3000,
                        pauseOnHover: true
                    }
                });
            });
            </script>
            <div id="slider">
			<?php
            $sql = 'SELECT
					  filename
					FROM
					  images
					WHERE
					  school_id = '.$row_school['school_id'].'
					ORDER BY
					  image_id
				';
			$result = query($sql);
			while ($row_image = mysql_fetch_object($result)) {
			?>
            	<img src="<?=HTML_BASE.'uploads/img/'.$row_image->filename?>" />
            <?php
			}
			?>
            </div>
            
        </div>
        
        <div class="right">
        	<h2>Location</h2>
            
            <div class="padding">
                <div class="small">
                    <?=$row_school['address']?>
                </div>
            </div>
        </div>
        
        <div class="right">
        	<h2>Map</h2>
            
            <div id="map" style="width:100%; height:250px;">
            	<script type="text/javascript">
					initialize("<?=$row_school['lat']?>","<?=$row_school['lng']?>");
				</script>
            </div>
        </div>
        
    	<div id="content">
        	
			<select class="inputbox" id="currency-down">
				<option value="USD" <?=($_SESSION["currency"]=='USD')?"selected":""?> >$ USD</option>
				<option value="GBP" <?=($_SESSION["currency"]=='GBP')?"selected":""?> >£ GBP</option>
				<option value="EUR" <?=($_SESSION["currency"]=='EUR')?"selected":""?> >€ EUR</option>
			</select>
			
        	<h2>Available courses</h2>
            <br>
			<a name="courses"></a>
            
            
<script type="text/javascript">
$(document).ready(function() {
	$(".description").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
});
</script>           
            <div class="padding">
            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="sef_url" value="<?=$sef_url?>" />
            <input type="hidden" name="school_id" value="<?=$row_school['school_id']?>" />
            
            <table class="listing" width="100%" cellpadding="0" cellspacing="0">
                <thead>
                    <tr>
                        <td>Course Name</td>
                        <td>Start Date</td>
                        <td>Duration &amp; Tuition</td>
                        <td>Application fee</td>
                        <td>&nbsp;</td>
                    </tr>
                </thead>
                <tbody>
                <?php
          
                  $sql = "SELECT
						c.course_id,
						c.name,
						c.description,
						c.registration_fee,
						t.name AS type
					FROM
						courses AS c,
						course_types AS t
					WHERE
						c.course_type_id = t.course_type_id
					AND
						c.published
					AND	
						school_id = ".$row_school['school_id']."
					  ";
					$result = query($sql);
						  
					$i=1;
					while ($row_course = mysql_fetch_object($result)) {	  
                ?>
                    <tr class="<?=(($i%2)?"none":"zebra")?>">
                        
                        <td>
                            <a class="description" href="#desc<?=$i?>" title="<?=$row_course->name?>"><?=$row_course->name?></a>
                            
                            <input type="hidden" name="course_id" value="<?=$row_course->course_id?>" />
                            
                            <div style="display:none;">
                                <div id="desc<?=$i?>" style="width:700px;height:500px;overflow:auto">
                                    <?=$row_course->description?>
                                </div>
                            </div>
                            
                        </td>
                        
                        
                        <td>
                        <select class="inputbox" style="width:120px;" name="course_start_date"
                        onChange="window.location.replace(this.options[this.selectedIndex].value)">
                        <option value="0">Select</option>
                        <?php
                        $sql = "SELECT
									course_date_id,
                                    start_date
                                FROM
                                    course_dates
                                WHERE
                                    course_id = ".$row_course->course_id."
								AND
									start_date > '".date("Y-m-d H:i:s")."'
								GROUP BY
									start_date
                                ORDER BY
                                    start_date
                                  ";
                        $rs_date = query($sql);
                        while ($row_dates = mysql_fetch_object($rs_date)) {
                            echo '<option '
							.(($_GET['start_date']==$row_dates->start_date && $_GET['course_id']==$row_course->course_id)?'selected':'')
							.' value="'.HTML_BASE.'school/'.$sef_url.'&amp;start_date='.$row_dates->start_date.'&amp;course_id='.$row_course->course_id.'#courses">'
							.$row_dates->start_date.
							'</option>
							';
                        }
                        ?>
                        </select>
                    	</td>
                        <td>
                        
                    <?php if ($_GET['start_date'] && $_GET['course_id']==$row_course->course_id) { ?>
                        
                        <select class="inputbox" style="width:180px;" name="course_date_id">
                        <option value="0">Select</option>
                        <?php
                        $sql = "SELECT
									course_date_id,
                                    duration,
									tuition
                                FROM
                                    course_dates
                                WHERE
                                    course_id = ".$row_course->course_id."
								AND
									start_date = '".$_GET['start_date']."'
                                ORDER BY
                                    duration
                                  ";
                        $rs_dt = query($sql);
                        while ($row_dt = mysql_fetch_object($rs_dt)) {
                            echo '<option value="'.$row_dt->course_date_id.'">'.$row_dt->duration.' weeks, '.$_SESSION["curr_sim"].round($row_dt->tuition*$_SESSION["currency_value"]).'</option>
							';
                        }
                        ?>
                        </select>
                    
                    <?php } ?>
                    	&nbsp;
                        
                        </td>
                                                
                        <td><?=$_SESSION["curr_sim"]?><?=round($row_course->registration_fee*$_SESSION["currency_value"])?></td>
                             
                        
                        <td style="text-align:right"><button type="submit" class="button" name="button" >Apply</button></td>                                                           
                    </tr>
                    
                    
                    
                    
                 <?php
                      $i++;    
                    } 
                 ?>	
                      
                </tbody>
            </table>
            </form>
                          
            </div>
        </div>
	
    
     
     
           
       
       <div class="panel" style="position:relative;">
       
         <h2>Reviews</h2>
         <a name="reviews"></a>
         <br>
         
            <?php if ($_SESSION['USER_SID']){
				$sql_review = "SELECT user_id FROM schools_reviews
				WHERE user_id = '".$_SESSION['USER_SID']."'
				AND	school_id = '".$row_school['school_id']."'";
				
				if(!mysql_num_rows(query($sql_review)))
				{
			?>
            
           	<div id="tab-reating">
           	<h3>Write a review for <?=sth($row_school['name'])?></h3><br>
           
            <form id="form1" action="<?=HTML_BASE.'school/'.$sef_url.'#reviews'?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_review" value="1" />
            <input type="hidden" name="sef_url" value="<?=$sef_url?>" />
            <input type="hidden" name="school_id" value="<?=$row_school['school_id']?>" />
            
            <?php if ($error_message) { ?>
                <div class="red">
                    <?=$error_message?>
                </div>
            <?php } ?>
                
			<?php if ($message) { ?>
            <div class="red">
                <?=$message?>
            </div>              	
            <?php } ?>
            <table  style="width:100%; background:#F2F7FF;">
            
            <tr>
             <td rowspan="3" width="100" style="text-align:center; vertical-align: middle"><?=get_user_profile($_SESSION['USER_SID'])?></td>
                <td>Teacher Quality</td>
                <td>
				<input class="star required" type="radio" name="rating1" value="1" <?php if($_POST["rating1"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating1" value="2" <?php if($_POST["rating1"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating1" value="3" <?php if($_POST["rating1"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating1" value="4" <?php if($_POST["rating1"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating1" value="5" <?php if($_POST["rating1"] == 5) echo 'checked="checked"';?>/>
               	</td>
                    
                <td>Social Activities</td>
               	<td>
				<input class="star required" type="radio" name="rating3" value="1" <?php if($_POST["rating3"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating3" value="2" <?php if($_POST["rating3"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating3" value="3" <?php if($_POST["rating3"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating3" value="4" <?php if($_POST["rating3"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating3" value="5" <?php if($_POST["rating3"] == 5) echo 'checked="checked"';?>/>
                </td>
                
                <td>Location</td>
                <td>
                <input class="star required" type="radio" name="rating5" value="1" <?php if($_POST["rating5"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating5" value="2" <?php if($_POST["rating5"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating5" value="3" <?php if($_POST["rating5"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating5" value="4" <?php if($_POST["rating5"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating5" value="5" <?php if($_POST["rating5"] == 5) echo 'checked="checked"';?>/>
                </td>
                
           </tr>
               <tr >
               
               	<td>Personal Attention</td>
               	<td>
				<input class="star required" type="radio" name="rating2" value="1" <?php if($_POST["rating2"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating2" value="2" <?php if($_POST["rating2"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating2" value="3" <?php if($_POST["rating2"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating2" value="4" <?php if($_POST["rating2"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating2" value="5" <?php if($_POST["rating2"] == 5) echo 'checked="checked"';?>/>
               
                </td>
               
                <td>Facilities</td>
                <td>
				<input class="star required" type="radio" name="rating4" value="1" <?php if($_POST["rating4"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating4" value="2" <?php if($_POST["rating4"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating4" value="3" <?php if($_POST["rating4"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating4" value="4" <?php if($_POST["rating4"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating4" value="5" <?php if($_POST["rating4"] == 5) echo 'checked="checked"';?>/>
                </td>
                    
                    
                    
                <td>City Safety</td>
                <td>
				<input class="star required" type="radio" name="rating6" value="1" <?php if($_POST["rating6"] == 1) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating6" value="2" <?php if($_POST["rating6"] == 2) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating6" value="3" <?php if($_POST["rating6"] == 3) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating6" value="4" <?php if($_POST["rating6"] == 4) echo 'checked="checked"';?>/>
                <input class="star" type="radio" name="rating6" value="5" <?php if($_POST["rating6"] == 5) echo 'checked="checked"';?>/>
                </td>
                    
                  </tr>
              <tr >
                	<td colspan="7" >
                    	<br>
                    	What did you think of <?=sth($row_school['name'])?>?<br>
						<textarea  style="width:95%"   class="inputbox"  name="review" rows="7"><?=$_POST['review']?></textarea>
              			<input style="float:right" class="button" type="submit" value="Submit review" />  
                    </td>
              </tr>
            
               
               
             </table>
            </form>
            
            <br><hr><br>
            
			</div>
            
            
            
            <?php
				}
				
			}else{
				
				$_SESSION['LOGIN_REDIRECT'] = 'school/'.$sef_url.'#reviews';
			?>	
            	<a style="position:absolute; right:12px; top:12px;" class="button" href="login.php">Submit Review</a>
			<?php	
			}
			?>
            
            
         	<form>
            <?php
          	
                  $sql = "SELECT
						user_id,
						fb_id,
						review,
						facilities,
						location,
						teacher,
						personal,
						social,
						safety
					FROM
						schools_reviews
					WHERE
					
						published
					AND	
						school_id = ".$row_school['school_id']."
						
					ORDER BY add_time DESC
					  ";
					$result = query($sql);
						  
					$i=1;
					while ($row_reviews = mysql_fetch_object($result)) {	  
                ?>
            <table width="100%">      
            <tr>
                <td rowspan="3" width="100" style="text-align:center; vertical-align: middle"><?=get_user_profile($row_reviews->user_id)?></td>
                <td>
                    Teacher Quality</td><td>
                    <?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->teacher) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-1-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
                <td>
                    Social Activities</td><td>
                    <?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->social) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-3-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
                <td>
                	Location</td><td>
					<?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->location) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-5-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
            </tr>
            <tr>
                <td>
                    Personal Attention</td><td>
                    <?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->personal) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-2-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
                <td>
                	Facilities</td><td>
					<?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->facilities) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-4-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
                <td>
                	City Safety</td><td>
					<?php for($u=1; $u<6; $u++)
                    {   
                        $ch = "";
                        if($u == $row_reviews->safety) $ch = 'checked="checked"';
                    ?>
                        <input class="star" type="radio" name="rating-6-<?=$i?>" value="<?=$u?>" disabled="disabled" <?=$ch?> />
                    <?php
                    }?>
                </td>
            </tr>
            <tr>
            	<td colspan="6">
                	<p><?=$row_reviews->review?></p>
                </td>
            </tr>
          </table>
          
          <br><hr><br>
            
                <?php $i++; }?>
         
          </form>   
       
       </div> 
            
        
<script src="<?=HTML_BASE_SSL?>js/jquery.rating.pack.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
	$(function(){ 
	 $('#form1 .star').rating(); 
	});
</script>
<?php foot(); ?>